using System;
using Microsoft.SPOT;

namespace G2Tech.SharedContext
{
    /// <summary>
    /// Communication Channel base class
    /// </summary>
    public abstract class CommunicationChannelBase : ICommunicationChannel
    {
        #region Members

        protected DeviceAddress _localAddress = null;
        protected DeviceAddress _endPointAddress = null;
        
        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public CommunicationChannelBase(DeviceAddress localAddress, DeviceAddress endPointAddress, LinkCost cost)
        {
            IsStarted = false;

            _localAddress = localAddress;
            _endPointAddress = endPointAddress;
            
            this.Cost = cost;

            Status = LinkStatus.Enabled;
        } 

        #endregion

        #region Properties

        /// <summary>
        /// Gets or Sets the channel name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or Sets if the channel is started or not
        /// </summary>
        internal bool IsStarted { get; set; }

        /// <summary>
        /// Get the Channel Local Address
        /// </summary>
        public DeviceAddress LocalAddress 
        {
            get 
            {
                return _localAddress; 
            } 
        }

        /// <summary>
        /// Get the Channel EndPoint Address
        /// </summary>
        public DeviceAddress EndPointAddress
        {
            get
            {
                return _endPointAddress;
            }
        }

        /// <summary>
        /// The Link is Activated by the spanning tree??
        /// </summary>
        public LinkStatus Status { get; set; }

        /// <summary>
        /// Link Cost
        /// </summary>
        public LinkCost Cost { get; internal set; } 

        #endregion

        #region Start / Stop
        
        /// <summary>
        /// Start the Channel
        /// </summary>
        public virtual void Start()
        {
            IsStarted = true;
        }

        #endregion

        /// <summary>
        /// Send an update to the channel
        /// </summary>
        /// <param name="item"></param>
        public abstract void SendItem(ContextItem item);
        
        #region Events

        /// <summary>
        /// raise the in item received event!!
        /// </summary>
        /// <param name="item"></param>
        protected void raiseOnItemReceivedEvent(ContextItem item)
        {
            if (OnItemReceived != null)
            {
                OnItemReceived(this, item);
            }
        }
        
        /// <summary>
        /// On Item Received event
        /// </summary>
        public event CommunicationChannelDelegates.CommunicationChannelItemReceivedEventHandler OnItemReceived; 

        #endregion
    }
}
